package de.dopichaj.labrador.search.erg;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.lang.Validate;
import org.apache.commons.lang.builder.HashCodeBuilder;

public final class ErgNode {

    public enum Type { CATEGORY, TAG }
    private final String name;
    private final Type type;
    private final double coherence;
    private final Set<ErgNode> parents;
    private final Set<ErgNode> children;
    /**
     * Our null object.
     */
    public static final ErgNode ERGNODE_NULL =
        new ErgNode("NULL", Type.CATEGORY,
            ElementRelationshipGraph.MINIMUM_COHERENCE_VALUE);
    
    public ErgNode(String name, Type type, double coherence) {
        this.name = name;
        this.type = type;
        this.coherence = coherence;
        this.parents = new HashSet<ErgNode>();
        this.children = new HashSet<ErgNode>();
    }

    void addChild(ErgNode child) {
        children.add(child);
    }

    void addParent(ErgNode parent) {
        parents.add(parent);
    }

    public boolean isCategory() {
        return type == Type.CATEGORY;
    }

    public double coherence() {
        return coherence;
    }

    public Set<ErgNode> parents() {
        return Collections.unmodifiableSet(parents);
    }

    /**
     * Finds the ancestor category that has the highest 
     * coherence value.
     * @return The ancestor with the highest coherence value, or
     *   {@link #ERGNODE_NULL} if the node has no parents.
     */
    public ErgNode getAncestorWithMaxCoherence() {
        
        ErgNode best = ERGNODE_NULL;
        for (ErgNode parent : parents()) {
            double coherence = parent.coherence();
            if (coherence > best.coherence()) {
                best = parent;
            }
        }
        return best;
    }


    public Set<ErgNode> children() {
        return Collections.unmodifiableSet(children);
    }
    
    @Override
    public String toString() {
        return name + "/" + type + "/" + coherence;
    }

    public String name() {
        return name;
    }

    /**
     * Get all the nodes in this category (including this node).
     * @return All descendant nodes of the category, plus the node itself.
     */
    public Set<ErgNode> getNodesInCategory() {
        
        Set<ErgNode> elementsInCategory = getDescendants();
        elementsInCategory.add(this);
        return Collections.unmodifiableSet(elementsInCategory);
    }


    /**
     * Get the descendants of this node.
     * @return All descendant elements of the element, including the element itself
     *         (note: if element is not contained in the ERG, it is still returned.).
     */
    public Set<ErgNode> getDescendants() {
        
        return getDescendantsRecursive(this);
    }

    /**
     * Recursive method that is called from getDescendants.
     * @return All descendants of the category.
     */
    private Set<ErgNode> getDescendantsRecursive(ErgNode node) {
        
        Validate.notNull(node);
        
        Set<ErgNode> children = node.children();
        Set<ErgNode> result = new HashSet<ErgNode>(children);
        for (ErgNode child : children) {
            assert child != this : "Cycle in ERG not allowed";
            result.addAll(getDescendantsRecursive(child));
        }
        return result;
    }


    @Override
    public boolean equals(Object o) {
        if (o == null || !(o instanceof ErgNode)) {
            return false;
        } else {
            ErgNode n = (ErgNode)o;
            return name.equals(n.name()) && type.equals(n.type) &&
                coherence == n.coherence;
        }
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder(17, 37)
            .append(name).append(type).append(coherence).toHashCode();
    }
}
/*
Copyright (c) 2005 Benedikt Eger
Copyright (c) 2005-2007 Philipp Dopichaj

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/